#!/bin/bash
# -*- mode: shell-script; mode: flyspell-prog; -*-
#
# Copyright (c) 2010, Tadashi G Takaoka
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in
#   the documentation and/or other materials provided with the
#   distribution.
# - Neither the name of Tadashi G. Takaoka nor the names of its
#   contributors may be used to endorse or promote products derived
#   from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
#

. $(dirname ${BASH_ARGV[0]})/func.subr

fetch="-none-"
which wget >/dev/null && fetch="wget"
which curl >/dev/null && fetch="curl"
[[ $fetch = "-none-" ]] && die "missing wget or curl"

function fetch() {
    [[ $# -eq 2 ]] || die "usage: fetch url out_file"
    local url=$1 out=$2
    if [[ $fetch = "wget" ]]; then
        $fetch -o $out $url \
            || die "can not fetch $url"
    elif [[ $fetch = "curl" ]]; then
        $fetch -o $out -L $url \
            || die "can not fetch $url"
    else
        die "unknown fetch method: $fetch"
    fi
}

which cvs >/dev/null || die "missing cvs"
which svn >/dev/null || die "missing svn"
which git >/dev/null || die "missing git"

scriptdir=$(absolute_path $(dirname $0))
scriptname=$(basename $0 .sh)

. $scriptdir/config.subr

buildtop=$PWD
builddir=$PWD/build-$target-$scriptname

function usage {
    die "usage: $0 [--help|-h] [download|build|install|cleanup...]"
}

function main() {
    local i cmd
    local -a cmds

    for i in "$@"; do
        case $i in
            -h|--help) usage;;
            -*) die "unkown option $i, try --help";;
            *) cmds+=($i);;
        esac
    done

    PATH=$prefix/bin:$PATH
    if [[ ${#cmds[@]} -eq 0 ]]; then
        set -x; download && prepare && build; set +x
        echo "To install $scriptname under $prefix, run '$0 install'"
    else
        for cmd in "${cmds[@]}"; do
            case $cmd in
            download)
                set -x; download || die "download failed"; set +x;;
            build)
                set -x; prepare && build || die "build failed"; set +x;;
            install)
                set -x; install || die "install failed"; set +x;;
            cleanup)
                set -x; cleanup; set +x;;
            *)
                die "unknown command '$cmd'";;
            esac
        done
    fi
}

# Local Variables:
# indent-tabs-mode: nil
# End:
# vim: set et ts=4 sw=4:
